# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Orion <systmkor@gmail.com>
# Maintainer: Orion <systmkor@gmail.com>
#
# secfixes:
#   2.7.4-r0:
#     - CVE-2018-0732
#     - CVE-2018-0495
#   2.5.3-r1:
#     - CVE-2017-8301
#
pkgname=libressl
pkgver=3.1.3
_namever=${pkgname}${pkgver%.*}
pkgrel=0
pkgdesc="Version of the TLS/crypto stack forked from OpenSSL"
url="http://www.libressl.org/"
arch="all"
license="custom"
depends=""
pcprefix="libressl:" # avoid mix up with openssl pkg-config
makedepends_build="automake autoconf libtool"
makedepends_host="linux-headers"
makedepends="$makedepends_host $makedepends_build"
subpackages="$pkgname-dbg $_namever-libcrypto:_libs $_namever-libssl:_libs
	$_namever-libtls:_libs $pkgname-dev $pkgname-doc"
source="https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/$pkgname-$pkgver.tar.gz
	starttls-ldap.patch
	ssl-libcompat.patch
	s_client-add-options-verify_.patch
	"
builddir="$srcdir/$pkgname-$pkgver"

prepare() {
	default_prepare

	cd "$builddir"
	autoreconf -vif
}

build() {
	cd "$builddir"

	CFLAGS="-L$CBUILDROOT/usr/lib $CFLAGS" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--with-sysroot=$CBUILDROOT \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var
	make
}

check() {
	cd "$builddir"
	make -j1 check
}

package() {
	cd "$builddir"

	make DESTDIR="$pkgdir" install
	install -Dm644 COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING

	cd "$pkgdir"

	# /usr/bin/openssl will be replaced with openssl's binary. We provide
	# a /usr/bin/libressl binary in case something needs it.
	mv usr/bin/openssl usr/bin/libressl

	# Provided by ca-certificates-cacerts
	rm etc/ssl/cert.pem
	rmdir etc/ssl/certs

	# Provided by openssl's libcrypto1.1
	rm etc/ssl/openssl.cnf
}

_libs() {
	local name="${subpkgname#$_namever-}"
	pkgdesc="libressl $name library"
	depends=""

	mkdir -p "$subpkgdir"/lib "$subpkgdir"/usr/lib
	local f; for f in "$pkgdir"/usr/lib/$name.so.*; do
		mv $f "$subpkgdir"/lib/
		ln -s ../../lib/${f##*/} "$subpkgdir"/usr/lib/${f##*/}
	done
	if [ "$name" = "libcrypto" ]; then
		mv "$pkgdir"/etc "$subpkgdir"
	fi
}

sha512sums="f9f497fe5d09b8b762175da038a8e67d9d2567462fefc49e434d306a3071ba3a4d369c89143d9d90e3bbcc7b9ba051c9c923a0dfc41d856edc08ae59fa727ee8  libressl-3.1.3.tar.gz
9f1628fbc2a697b6570353920d784b161ca0a122047066d8bee15225bad1e5271aa2ed72b145506bcd4ffe58b35da2caf38c4a048db7e014dabd16b5eba44581  starttls-ldap.patch
18e72b7db28664ee4e294a4a461e48daa90be9e9323584112a5878fd56a578d02f4730c7c788f57cc39b2bc35447df0a47a4391077d764d953419890ad511d35  ssl-libcompat.patch
4c992872addbe4fd612ba9e3f859b5ba69b448aafa7676751ca7ca09bbcfc47a2a1cad468c235f8d1a65c65e8efb38f27c512a32b444346c39ec0d8dcfbcd346  s_client-add-options-verify_.patch"
